<template>
  <div class="p-2">
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5" v-if="hospitalList.length === 0">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:hospital:add']">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:hospital:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:hospital:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:hospital:export']">导出</el-button>
          </el-col> -->
          <right-toolbar :search="false" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="hospitalList" @selection-change="handleSelectionChange">
        <!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="名称" align="center" prop="title" />
        <el-table-column label="头像" align="center" prop="photoUrl" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.photoUrl" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="轮播图" align="center" prop="bannerUrl" width="100">
          <template #default="scope">
            <div v-for="(item, index) in scope.row?.bannerUrl?.split(',')" :key="index" class="banner-item flex">
              <image-preview :src="item" :width="50" :height="50" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="经度" align="center" prop="longitude" />
        <el-table-column label="纬度" align="center" prop="latitude" />
        <el-table-column label="医院地址" align="center" prop="address" />
        <el-table-column label="医院概况图" align="center" prop="overviewUrl" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.overviewUrl" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="来院导航图" align="center" prop="backgroundImageUrl" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.backgroundImageUrl" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['system:hospital:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['system:hospital:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改医院对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="50%" append-to-body>
      <el-form ref="hospitalFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="服务协议" prop="service">
          <editor v-model="form.service" />
        </el-form-item>
        <el-form-item label="隐私协议" prop="service">
          <editor v-model="form.privacy" />
        </el-form-item>
        <el-form-item label="关于我们" prop="about">
          <editor v-model="form.about" />
        </el-form-item>
        <el-form-item label="LOGO" prop="photo">
          <image-upload v-model="form.photo" :limit="1" />
        </el-form-item>
        <el-form-item label="轮播图" prop="banner">
          <div>
            <image-upload v-model="form.banner" :limit="5" />
            <div class="text-sm">建议尺寸为 365px x 130px</div>
          </div>
        </el-form-item>
        <el-form-item label="医院地址经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入医院地址经度" />
        </el-form-item>
        <el-form-item label="医院地址纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入医院地址纬度" />
        </el-form-item>
        <el-form-item label="医院概况图" prop="overview">
          <div>
            <image-upload v-model="form.overview" :file-size="20" :limit="1" />
            <div class="text-sm">建议尺寸为宽 365px，高 不限</div>
          </div>
        </el-form-item>
        <el-form-item label="来院导航图" prop="backgroundImage">
          <div>
            <image-upload v-model="form.backgroundImage" :limit="1" :file-size="20" />
            <div class="text-sm">建议尺寸为宽 365px，高 不限</div>
          </div>
        </el-form-item>
        <el-form-item label="医院详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入医院详细地址" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Hospital" lang="ts">
import { listHospital, getHospital, delHospital, addHospital, updateHospital } from '@/api/system/hospital';
import { HospitalVO, HospitalQuery, HospitalForm } from '@/api/system/hospital/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const hospitalList = ref<HospitalVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const hospitalFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: HospitalForm = {
  id: undefined,
  title: undefined,
  photo: undefined,
  banner: undefined,
  longitude: undefined,
  latitude: undefined,
  overview: undefined,
  backgroundImage: undefined,
  address: undefined,
  privacy: undefined,
  service: undefined,
  about: undefined
};
const data = reactive<PageData<HospitalForm, HospitalQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: undefined,
    service: undefined,
    about: undefined,
    photo: undefined,
    address: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键id不能为空', trigger: 'blur' }],
    title: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    service: [{ required: true, message: '服务协议不能为空', trigger: 'blur' }],
    privacy: [{ required: true, message: '隐私协议不能为空', trigger: 'blur' }],
    about: [{ required: true, message: '关于我们不能为空', trigger: 'blur' }],
    photo: [{ required: true, message: '头像不能为空', trigger: 'blur' }],
    banner: [{ required: true, message: '轮播图不能为空', trigger: 'blur' }],
    address: [{ required: true, message: '医院详细地址不能为空', trigger: 'blur' }],
    longitude: [{ required: true, message: '经度不能为空', trigger: 'blur' }],
    latitude: [{ required: true, message: '纬度不能为空', trigger: 'blur' }],
    overview: [{ required: true, message: '简介不能为空', trigger: 'blur' }],
    backgroundImage: [{ required: true, message: '来院导航图不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询医院列表 */
const getList = async () => {
  loading.value = true;
  const res = await listHospital(queryParams.value);
  hospitalList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  hospitalFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: HospitalVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加医院';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: HospitalVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getHospital(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改医院';
};

/** 提交按钮 */
const submitForm = () => {
  hospitalFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateHospital(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addHospital(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: HospitalVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除医院编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delHospital(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/hospital/export',
    {
      ...queryParams.value
    },
    `hospital_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
